package com.academic.system.service.impl;

import com.academic.common.core.domain.entity.SysCourse;
import com.academic.common.core.domain.entity.UserGrade;
import com.academic.common.core.page.TableDataInfo;
import com.academic.system.domain.rep.GradeRep;
import com.academic.system.domain.req.NewReq;
import com.academic.system.mapper.dao.SysCourseDao;
import com.academic.system.mapper.dao.UserGradeDao;
import com.academic.system.service.UserGradeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (UserGrade)表服务实现类
 *
 * @author makejava
 * @since 2024-10-31 22:55:00
 */
@Service("userGradeService")
public class UserGradeServiceImpl extends ServiceImpl<UserGradeDao, UserGrade> implements UserGradeService {

    private final UserGradeDao userGradeDao;
    private final SysCourseDao sysCourseDao;
    private final SysBookServiceImpl sysBookService;

    public UserGradeServiceImpl(UserGradeDao userGradeDao, SysCourseDao sysCourseDao, SysBookServiceImpl sysBookService) {
        this.userGradeDao = userGradeDao;
        this.sysCourseDao = sysCourseDao;
        this.sysBookService = sysBookService;
    }


    @Override
    public TableDataInfo getGradeHistory(NewReq newReq) {
        PageHelper.startPage(newReq.getPageNum(), newReq.getPageSize());
        List<GradeRep> gradeReps = new ArrayList<>();
        QueryWrapper<UserGrade> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", newReq.getId());
        List<UserGrade> userGrades = userGradeDao.selectList(queryWrapper);
        PageInfo<UserGrade> pageInfo = new PageInfo<>(userGrades);
        List<Long> courseIds = userGrades
                .stream()
                .map(UserGrade::getCourseId)
                .collect(Collectors.toList());
        QueryWrapper<SysCourse> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("id", courseIds);
        List<SysCourse> sysCourses = sysCourseDao.selectList(queryWrapper1);
        for (UserGrade userGrade : userGrades) {
            GradeRep gradeRep = new GradeRep();
            SysCourse sysCourse = sysCourses.stream()
                    .filter(course -> course.getId()
                            .equals(userGrade.getCourseId()))
                    .findFirst()
                    .get();
            gradeRep.setCourseId(sysCourse.getId());
            gradeRep.setCourseName(sysCourse.getCourseName());
            gradeRep.setTeacherName(sysCourse.getTeacherName());
            gradeRep.setExamYears(userGrade.getExamYears());
            gradeRep.setOrdinary(userGrade.getOrdinary());
            gradeRep.setExperiment(userGrade.getExperiment());
            gradeRep.setFinalExams(userGrade.getFinalExams());
            gradeRep.setTotalScore(userGrade.getTotalScore());
            gradeReps.add(gradeRep);
        }
        return sysBookService.getDataTable(gradeReps,pageInfo.getTotal());
    }
}

